import pandas as pd
import numpy as np

data = {
    "日期": pd.date_range("2024-01-01", periods=10, freq="D"),
    "产品": ["手机", "笔记本", "平板", "手机", "笔记本", "平板", "手机", "笔记本", "平板", "手机"],
    "销量": [100, 80, 60, 110, np.nan, 75, 95, 95, 70, 50],
    "单价": [3000, 5000, 2500, 3100, 4900, np.nan, 3050, 5200, 2600, 2950]
}

df = pd.DataFrame(data)

print("\n缺失值统计：")
# 检测缺失值
print(df.isnull().sum())

# 安全填充：销量用均值，单价用前向填充
df["销量"] = df["销量"].fillna(df["销量"].mean())
df["单价"] = df["单价"].ffill()

# 替换异常值（销量>150 视为异常，替换为中位数）
median_sales = df["销量"].median()
df.loc[df["销量"] > 150, "销量"] = median_sales